package cn.kilo.domain.service.order.impl;

import cn.kilo.domain.model.order.entity.OrderStaffEntity;
import cn.kilo.domain.model.order.entity.StaffEntity;
import cn.kilo.domain.model.order.vo.OrderStatusVO;
import cn.kilo.domain.service.order.AbstractStaffStrategyChain;
import cn.kilo.domain.model.order.entity.OrderEntity;
import cn.kilo.domain.service.order.factory.DefaultFactory;
import cn.kilo.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author kilok
 * 分拣员2
 */
@Slf4j
@Component("sorter2")
@Transactional
public class Sorter2Strategy extends AbstractStaffStrategyChain {
    @Override
    public DefaultFactory.Role getRole() {
        return DefaultFactory.Role.SORTER2;
    }

    @Override
    public Map<String, Integer> getLeftStaff(OrderEntity order) {
        return repository.getSorter2Staff(order);
    }

    @Override
    public boolean beStaff(StaffEntity staffEntity) {
        boolean res=repository.checkSorter2(staffEntity);
        if(res){
            return false;
        }
        return repository.beSorter2(staffEntity);
    }


    @Override
    public List<OrderStatusVO> doJob(OrderEntity order) {
        String sorter2Id = order.getSorter2();
        String staffName = repository.getStaffNameById(sorter2Id);
        String cacheKey = Constants.RedisKey.ORDER_INFO + Constants.UNDERLINE + order.getOrderId();
        OrderStatusVO orderStatusVO = repository.getOrderStatusEntity(cacheKey);

        if (null == orderStatusVO.getNow()) {
            String guardName = orderStatusVO.getStaffName();
            String guard=repository.getStaffIdByName(guardName);
            repository.deleteStaffOrderInfo(guard,orderStatusVO);
            //接取订单
            log.info("送至寝室");
            order.setSorter2(sorter2Id);
            //开始计时
            orderStatusVO.setStatusCode(DefaultFactory.Status.TAKE_3.getCode());
            orderStatusVO.setStatus(DefaultFactory.Status.TAKE_3.getInfo());
            orderStatusVO.setStaffName(staffName);
            orderStatusVO.setNow(LocalDateTime.now());

            String staffKey = Constants.RedisKey.STAFF_ORDER + Constants.UNDERLINE + sorter2Id;
            orderStock.addOrderStaffStockQueue(OrderStaffEntity
                    .builder().orderId(order.getOrderId())
                    .staffRole(DefaultFactory.Role.GUARD.getCode())
                    .staffName(guardName)
                    .time(0L).distance(0)
                    .end(orderStatusVO.getEnd())
                    .build());
            repository.updateOrder(orderStatusVO, staffKey, sorter2Id);
            //guard按照时间计费就不对guard和订单进行操作了
            List<OrderStatusVO> list=new ArrayList<>();
            list.add(orderStatusVO);
            return list;
        } else {
            //结束订单
            log.info("配送员结束订单");
            //这里要先判断是否上传了图片
            List<OrderStatusVO> list=new ArrayList<>();
            if (!repository.checkPicture(orderStatusVO.getOrderId())) {
                orderStatusVO.setStatus(DefaultFactory.Status.FAIL_OVER_ODER.getInfo());
                orderStatusVO.setStatusCode(DefaultFactory.Status.FAIL_OVER_ODER.getCode());
                list.add(orderStatusVO);
                return list;
            }
            repository.deleteStaffOrderInfo(sorter2Id,orderStatusVO);
            String staffKey = Constants.RedisKey.STAFF_ORDER + Constants.UNDERLINE + sorter2Id;
            //计时结束
            orderStatusVO.setEndTime(LocalDateTime.now());
            orderStatusVO.setTimer(ChronoUnit.MINUTES.between(orderStatusVO.getNow(), LocalDateTime.now()));
            orderStatusVO.setStatus(DefaultFactory.Status.OVER.getInfo());

            String removeKey = Constants.RedisKey.STAFF_ORDER + Constants.UNDERLINE + order.getSorter2();
            repository.removeSortedSetOrderInfo(order.getOrderId(), removeKey);
            repository.updateOrder(orderStatusVO, staffKey,order.getSorter2());
            orderStock.addOrderStaffStockQueue(OrderStaffEntity.builder().orderId(order.getOrderId()).staffName(staffName).distance(0).time(orderStatusVO.getTimer()).end(orderStatusVO.getEnd()).staffRole(DefaultFactory.Role.SORTER2.getCode()).build());
            list.add(orderStatusVO);
            return list;

        }
    }
}